/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pg.misra.rules;

import java.util.ArrayList;
import java.util.List;
import pl.pg.misra.AST.AST;
import pl.pg.misra.Interfaces.IRule;

/**
 *
 * @author Paweł
 */
public class CompoundStatementRule extends AbstractRule implements IRule{

    @Override
    public List<String> checkRule(List<AST> asts) {
        List<String> result = new ArrayList<>();
        List<AST> searchResult;
        for (AST node : asts){
            searchResult = new ArrayList<>();
            
            searchResult = findNodesByType("ForStmt", node, searchResult);
            searchResult = findNodesByType("SwitchStmt", node, searchResult);
            searchResult = findNodesByType("DoStmt", node, searchResult);
            searchResult = findNodesByType("WhileStmt", node, searchResult);
            
            for (AST compoundNode : searchResult){
                AST searchEvaluation = findCompoundNodesInStatement(compoundNode);
                
                if (searchEvaluation == null){
                    result.add(buildViolationMessage(node, "14.8", "no brackets in statement"));
                }
            }
        }
        
        return result;
    }
    
    private AST findCompoundNodesInStatement(AST node){
        AST result = null;
        
        for (AST child : node.getChildrens()){
            if (child.getNodeType().equals("CompoundStmt")){
                result = child;
                break;
            }
        }
        
        return result;
    }
    
}
